package com.milkshopbackend.milkshop.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milkshopbackend.milkshop.Service.CartSelectionService;
import com.milkshopbackend.milkshop.entity.CartSelection;
import com.milkshopbackend.milkshop.mapper.CartSelectionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class CartSelectionServiceImpl extends ServiceImpl<CartSelectionMapper, CartSelection> implements CartSelectionService {

    @Override
    public void selectCart(Long userId, Long cartId) {
        CartSelection selection = new CartSelection();
        selection.setUserId(userId);
        selection.setCartId(cartId);
        this.save(selection);
    }

    @Override
    public void unselectCart(Long userId, Long cartId) {
        LambdaQueryWrapper<CartSelection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CartSelection::getUserId, userId)
                .eq(CartSelection::getCartId, cartId);
        this.remove(wrapper);
    }

    @Override
    public void selectAllCarts(Long userId) {
        // 这个方法需要配合 CartService 来实现
        // 因为需要知道用户的所有购物车项
        // 暂时留空，等待与 CartService 的集成
        throw new UnsupportedOperationException("此功能尚未实现");
    }

    @Override
    public void unselectAllCarts(Long userId) {
        LambdaQueryWrapper<CartSelection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CartSelection::getUserId, userId);
        this.remove(wrapper);
    }

    @Override
    public List<Long> getSelectedCartIds(Long userId) {
        LambdaQueryWrapper<CartSelection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CartSelection::getUserId, userId);
        return this.list(wrapper).stream()
                .map(CartSelection::getCartId)
                .collect(Collectors.toList());
    }
} 